package com.framework.modules.api;

import com.framework.common.utils.ConfigConstant;
import com.framework.dto.AcceptKaYouDTO;
import com.framework.dto.LishuaVipDTO;
import com.framework.entity.oss.SysOssEntity;
import com.framework.entity.sys.SysAgent;
import com.framework.exception.RRException;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.oss.service.SysOssService;
import com.framework.service.AgentService;
import com.framework.service.TerLishuaVipService;
import com.framework.service.TerminalInfoService;
import com.framework.service.TransactionInfoService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.ali.AliOssUtils;
import com.framework.utils.string.StringUtil;
import com.framework.view.LiShuaYajin;
import com.framework.view.LishuaTran;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

/**
 * 外部暴漏接口
 */
@RestController
@RequestMapping("api")
public class ApiController {
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private AgentService agentService;

    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private TerLishuaVipService terLishuaVipService;



    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public RespResult upload(MultipartFile file, String prefix, String token) throws Exception {
        if (file == null) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (file.isEmpty()) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (StringUtils.isBlank(prefix)) {
            throw new RRException("9000", "上传图片前缀不能为空");
        }
        //验证是否登录
        agentService.queryAgentByToken(token);
        // 上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);

        // 保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);
        return RespResult.ok().put("url", url);
    }

    /**
     * 导入乐刷大pos交易
     *
     * @param data
     * @param sign
     * @return
     */
    @RequestMapping("/addTransactionByLeShua")
    public String getTransaction(String data, String sign) {
        String str = transactionInfoService.addTransactionByLeShuaBig(data, sign);
        return str;
    }


    /**
     * 导入乐刷大pos交易
     *
     * @param data
     * @param sign
     * @return
     */
    @RequestMapping("/addTransactionByLeShuaSn")
    public String getLeshuaTransactionBySn(String data, String sign) {
        String str = transactionInfoService.addTransactionByLeShuaBigSn(data, sign);
        return str;
    }
    /**
     * 绑定台卡的终端号
     *
     * @return
     */
    @RequestMapping(value = "/bindSnAndTerminalCode", method = RequestMethod.POST)
    public RespResult bindSnAndTerminalCode(String merc_id,
                                            String merc_nm,
                                            String corg_merc_id,
                                            String bind_dt,
                                            String sn_no,
                                            String org_no,
                                            String trm_no,
                                            String SDTermNo,
                                            String TxnDate,
                                            String TxnAmt,
                                            String TxnStatus,
                                            String TxnLogId) {

        if(!StringUtil.isEmpty(merc_id)){
            Map<String, Object> map = terminalInfoService.bindSnAndTerminalCode(merc_id,
                    merc_nm,
                    corg_merc_id,
                    bind_dt,
                    sn_no,
                    org_no,
                    trm_no);
        }

        Map<String, Object> map =transactionInfoService.getStarPosTransaction(SDTermNo,
                TxnDate,
                TxnAmt,
                TxnStatus,
                TxnLogId);

        return RespResult.ok(map);
    }

    /**
     * 上传文件
     */
    @RequestMapping("/yhbUpload")
    public RespResult yhbUpload(MultipartFile file, String prefix, String token) throws Exception {
        if (file == null) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (file.isEmpty()) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (StringUtils.isBlank(prefix)) {
            throw new RRException("9000", "上传图片前缀不能为空");
        }
        //验证是否登录
        yhbUserService.queryUserByToken(token);
        // 上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);

        // 保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);
        return RespResult.ok().put("url", url);
    }


    /**
     * 导入立刷押金
     *
     * @param liShuaYajin
     * @return
     */
    @PostMapping("/getLishuaYajin")
    public Map<String,String> getLishuaYajin(@RequestBody LiShuaYajin liShuaYajin) {
        Map<String,String> map = transactionInfoService.getLishuaYajin(liShuaYajin);
        return map;
    }
    /**
     * 导入立刷交易
     *
     * @return
     */
    @PostMapping("/getLishuaTran")
    public Map<String,String> getLishuaTran(@RequestBody LishuaTran lishuaTran) {
        Map<String,String> map= transactionInfoService.getLishuaTran(lishuaTran);
        return map;
    }

    /**
     * 接受卡友数据
     * @param flowNo
     * @param timeStamp
     * @param acceptKey
     * @param sign
     * @param acceptKaYouDTO
     * @return
     */
    @PostMapping("/acceptKaYou")
    public Map<String,String> acceptKaYou(String flowNo,String timeStamp,String acceptKey,String sign,@RequestBody AcceptKaYouDTO acceptKaYouDTO){
        return transactionInfoService.acceptKaYou(flowNo,timeStamp,acceptKey,sign,acceptKaYouDTO);
    }


    /**
     * 上传文件
     */
    @RequestMapping("/uploadForyoufu")
    public RespResult uploadForyoufu(MultipartFile file,String prefix,String token) throws Exception {
        if (file == null) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (file.isEmpty()) {
            throw new RRException("9000", "上传文件不能为空");
        }

        //验证是否登录
        SysAgent agent = agentService.queryAgentByToken(token);
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        String objectName="identity/"+prefix+"_"+agent.getAgentId()+suffix;

        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(file.getBytes());

        AliOssUtils.uploadBytes(byteArrayInputStream,objectName);

        // 保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(objectName);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);
        return RespResult.ok().put("url", objectName);
    }

    /**
     * 更新立刷押金
     * @param lishuaVipDTO
     * @return
     */
    @PostMapping("/updateLishuaVip")
    public Map<String,String> updateLishuaVip(@RequestBody LishuaVipDTO lishuaVipDTO){
        Map<String,String> map=terLishuaVipService.updateLishuaVip(lishuaVipDTO);
        return map;
    }

}
